//
//  HYBaseThemeViewController.m
//  _normal
//
//  Created by Ezra on 16/6/12.
//  Copyright © 2016年 Ezra. All rights reserved.
//

#import "HYBaseThemeViewController.h"

#import <AFNetworking/AFNetworking.h>
#import <MJExtension/MJExtension.h>
#import <MJRefresh/MJRefresh.h>

#import "HYThemeItem.h"
#import "HYThemeCell.h"
#import "HYThemeViewModel.h"
#import "HYThemeParam.h"

//#import "HYNewViewController.h"

static NSString * ID = @"cell";
@interface HYBaseThemeViewController ()

//@property (nonatomic, strong)NSArray *itemArr;

@property (nonatomic, strong)NSMutableArray *themeViewModels;

@property (nonatomic, strong) AFHTTPSessionManager *mgr;

@property (nonatomic, strong) NSString *maxtime;

@property (nonatomic, weak) id observe;

@end

@implementation HYBaseThemeViewController
-(AFHTTPSessionManager *)mgr
{
    if (!_mgr) {
        _mgr = [AFHTTPSessionManager manager];
    }
    return _mgr;
}

-(NSMutableArray *)themeViewModels
{
    if (!_themeViewModels) {
        _themeViewModels = [NSMutableArray array];
    }
    return _themeViewModels;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.mgr.tasks makeObjectsPerformSelector:@selector(cancel)];
    self.tableView.contentInset = UIEdgeInsetsMake(64, 0, 49, 0);
    
    //    NSLog(@"%@",NSStringFromCGRect(self.tableView.frame));
    
    //注意这里用registerClass 不用nib，因为没有xib
    [self.tableView registerClass:[HYThemeCell class] forCellReuseIdentifier:ID];
    
    self.tableView.backgroundColor = HYCommonBgColor;
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    //设置tableview指示器额外滚动区域
    self.tableView.scrollIndicatorInsets = self.tableView.contentInset;
    
//    [self reloadNewData];
    
    [self setupRefreshView];
    
    [self.tableView.mj_header beginRefreshing];
    
    self.observe = [[NSNotificationCenter defaultCenter] addObserverForName:repeateClickTabBarButtonNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
        
        NSLog(@"来了多次？");
        
        //window 只要有一个控件显示到屏幕上，这个window就有值。
        if (self.view.window) {
            
            [self reloadData];
        
        }
    }];
}
//销毁通知
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self.observe];
}


- (void)setupRefreshView
{
    MJRefreshAutoNormalFooter *footerView = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    self.tableView.mj_footer = footerView;
    footerView.automaticallyHidden = YES;
    
    MJRefreshNormalHeader *headerView = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(reloadNewData)];
    self.tableView.mj_header = headerView;
    headerView.automaticallyChangeAlpha = YES;
}

- (void)reloadNewData
{
    //    NSMutableDictionary *paramenters = [NSMutableDictionary dictionary];
    //    paramenters[@"a"] = @"list";
    //    paramenters[@"c"] = @"data";
    //    paramenters[@"type"] = @10;
    
    [self.mgr.tasks makeObjectsPerformSelector:@selector(cancel)];
    
    HYThemeParam *param = [HYThemeParam param];
//    param.type = self.type;
    
    //新帖
//    if ([self.parentViewController isKindOfClass:[HYNewViewController class]]) {
//
//        param.a = @"newlist";
//
//    }
    
    
    
    NSString *url = [NSString stringWithFormat:@"%@%@",HYAPI,self.type];
    
    
    [self.mgr GET:url parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *  _Nullable responseObject) {
        
        [self.tableView.mj_header endRefreshing];
        
//      self.maxtime = responseObject[@"info"][@"maxtime"];
        
        NSArray *dictArr = responseObject[@"data"];
        
        NSArray *themeItems = [HYThemeItem mj_objectArrayWithKeyValuesArray:dictArr];
        
        NSMutableArray *arrM = [NSMutableArray array];
        
        for (HYThemeItem *item in themeItems) {

            HYThemeViewModel *vm = [[HYThemeViewModel alloc]init];
            vm.item = item;

            [arrM addObject:vm];
        }
        
        self.themeViewModels = arrM;
        
        [self.tableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
        [self.tableView.mj_header endRefreshing];
    }];
    
}

- (void)loadMoreData
{
    
    
    
    [self.tableView.mj_footer endRefreshing];
    [self.mgr.tasks makeObjectsPerformSelector:@selector(cancel)];

//    return;
//
//
//
//
//    HYThemeParam *param = [HYThemeParam param];
////    param.maxtime = self.maxtime;
//    param.type = self.type;
//
//    //新帖
////    if ([self.parentViewController isKindOfClass:[HYNewViewController class]]) {
////
////        param.a = @"newlist";
////
////    }
//
//    [self.mgr GET:HYAPI parameters:param.mj_keyValues progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//
//        //结束刷新状态
//        [self.tableView.mj_footer endRefreshing];
//
////        self.maxtime = responseObject[@"info"][@"maxtime"];
//        NSArray *dictArr = responseObject[@"data"];
//        //字典转模型
//        NSArray *themeItems = [HYThemeItem mj_objectArrayWithKeyValuesArray:dictArr];
//
//        for (HYThemeItem *item in themeItems) {
//
//            HYThemeViewModel *vm = [[HYThemeViewModel alloc]init];
//            vm.item = item;
//            [self.themeViewModels addObject:vm];
//
//        }
//        [self.tableView reloadData];
//
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        NSLog(@"%@",error);
//        //结束刷新状态
//        [self.tableView.mj_footer endRefreshing];
//
//    }];
    
    
    
}

- (void)reloadData
{
    [self.tableView.mj_header beginRefreshing];

}


#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    self.tableView.tableFooterView.hidden = !self.themeViewModels.count;
    return self.themeViewModels.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HYThemeCell *cell = [tableView dequeueReusableCellWithIdentifier:ID forIndexPath:indexPath];
    
    if (cell == nil) {
        
        cell = [[HYThemeCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        
    }
    
    cell.vm = self.themeViewModels[indexPath.row];
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [self.themeViewModels[indexPath.row] cellH];
}



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{
    
    HYThemeViewModel *vm = self.themeViewModels[indexPath.row];
    
    
    if([vm.item.filetype isEqualToString:@"txt"]){
        
        
//    [self.navigationController pushViewController: animated:NO];
        
    }else{
        
        
    }
}




@end
